package com.joycessh.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.joycessh.util.DBConnectionManager;

/**
 * @author joycessh
 * Servlet implementation class ArchiveQueryServlet
 */
@WebServlet("/ArchiveQueryServlet")
public class ArchiveQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	
	DBConnectionManager dcm = null;
	Connection conn = null;
	Statement  stmt = null;
	ResultSet rs = null;
	PreparedStatement prepstmt = null;
	String sqlStr ="";
	Boolean flag = false;
	int effectLines = 0;
	
    public ArchiveQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		request.setCharacterEncoding("utf8");
		
		String positionTypeName = request.getParameter("positionTypeName");
		String positionName = request.getParameter("positionName");
		
		int empId = 0;
		String archiveId = "";
		String empName = "";
		String sex = "";
		
		//List<String> list = new ArrayList<String>();
		
		dcm = new DBConnectionManager();
		try {
			conn = dcm.getConnection();
			sqlStr = "select emp_id from emp_position where position_type_name=? and position_name=?";
			prepstmt = conn.prepareStatement(sqlStr);
			prepstmt.setString(1, positionTypeName);
			prepstmt.setString(2, positionName);
			rs = prepstmt.executeQuery();
			if (rs.next()) {
				empId = rs.getInt(1);
				//list.add(empId);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		try {			
			sqlStr = "select id from emp_archive where emp_id=?";
			prepstmt = conn.prepareStatement(sqlStr);
			prepstmt.setInt(1, empId);
			rs = prepstmt.executeQuery();
			if (rs.next()) {
				archiveId = rs.getString(1);
				//list.add(archiveId);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {			
			sqlStr = "select emp_name, sex from employee where id=?";
			prepstmt = conn.prepareStatement(sqlStr);
			prepstmt.setInt(1, empId);
			rs = prepstmt.executeQuery();
			if (rs.next()) {
				empName = rs.getString(1);
				sex = rs.getString(2);
				//list.add(empName);
				//list.add(sex);
				//list.add(positionName);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			rs.close();
			prepstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//HttpSession session = request.getSession();
		/*request.getSession().setAttribute("archiveId", archiveId);
		request.getSession().setAttribute("empName", empName);
		request.getSession().setAttribute("sex", sex);
		request.getSession().setAttribute("positionName", positionName);*/
		
		List<String> list = new ArrayList<String>();
		list.add(archiveId);
		list.add(empName);
		list.add(sex);
		list.add(positionName);
		
		request.setAttribute("list", list);
		
		RequestDispatcher rd = request.getRequestDispatcher("query_result.jsp");
		rd.forward(request, response);
				
	}

}
